package com.zhihuichongcheng.application.ui.openclass.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.openclass.OpenClassCategoryBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/11/2.
 */

public class OpenClassCategoryAdapter extends RecyclerView.Adapter<OpenClassCategoryAdapter.ViewHold> {
    private Context mContext;
    private List<OpenClassCategoryBean> mListData = new ArrayList<>();
    private OnItemOnclick<OpenClassCategoryBean> onItemOnclick;
    private int width;
    public OpenClassCategoryAdapter(Context context)
    {
        this.mContext = context;
    }

    public void setData(List<OpenClassCategoryBean> list)
    {
        this.mListData = list;
    }

    public void setOnItemOnclick(OnItemOnclick<OpenClassCategoryBean> onItemOnclick)
    {
        this.onItemOnclick = onItemOnclick;
        width = DeviceUtil.getWidth(mContext);
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_openclass_category,null);
        return new ViewHold(view);

    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final OpenClassCategoryBean bean = mListData.get(position);
        if(mListData.size() >4)
        {
            holder.view.setLayoutParams(new GridLayoutManager.LayoutParams(width/9*2, ViewGroup.LayoutParams.MATCH_PARENT));
        }
        else {
            holder.view.setLayoutParams(new GridLayoutManager.LayoutParams(width/4, ViewGroup.LayoutParams.MATCH_PARENT));
        }

        Glide.with(mContext).load(bean.getCategoryLogo()).placeholder(R.drawable.ic_cricle_defalut).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);
        holder.mTvName.setText(bean.getCategoryName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemOnclick!=null)
                {
                    onItemOnclick.onClick(bean,position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
         return mListData.size();
    }



    class ViewHold extends RecyclerView.ViewHolder{
        private TextView mTvName;
        private View view;
        private ImageView mImAvatar;

        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mTvName = (TextView)itemView.findViewById(R.id.tv_name);
            mImAvatar = (ImageView)itemView.findViewById(R.id.im_avatar);
        }
    }
}
